package com.qian.ddj.web.controller;

import com.qian.api.ddj.service.IMailService;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.web.system.security.SecurityUtils;
import com.yaowk.web.system.security.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("ddj/mail")
@Api(tags = "邮件发送")
public class MailController {

    @Autowired
    IMailService mailService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private UserRepository dao;


    @PostMapping("/sendForgetEmail")
    @ApiOperation("忘记密码发送邮件")
    public void sendforgetEmail(String userName) {
        mailService.forgotPassWordEmail(userName);
    }

    @PostMapping("/sendBindEmail")
    @ApiOperation("绑定邮箱发送邮件")
    public void sendbindEmail(String email) {

        mailService.bindEmail(email, SecurityUtils.getUserInfo().getUsername());
    }


    @PostMapping("/forgotPassWotdCheckCode")
    @ApiOperation("验证忘记密码验证码是否正确")
    public void forgotPassWotdCheckCode(String userName, String passWord, String code) {
        passWord = passwordEncoder.encode(passWord);
        mailService.forgotPassWotdCheckCode(userName, passWord, code);
    }

    @PostMapping("/checkCode")
    @ApiOperation("验证绑定邮箱验证码是否正确")
    public void checkCode(String code) {
        SecurityUtils securityUtils = new SecurityUtils();
        UserInfo userInfo = securityUtils.getUserInfo();

        mailService.checkCode(code, userInfo.getUsername());

    }

    @GetMapping("/queryEmailAddressByUserName")
    @ApiOperation("验证邮箱是否绑定")
    public String queryEmailAddressByUserName() {
        SecurityUtils securityUtils = new SecurityUtils();
        UserInfo userInfo = securityUtils.getUserInfo();
        String userName = userInfo.getUsername();
       DdjUser ddjUser=  dao.findByUsername(userName);
        return ddjUser.getEmail();
    }
}